package GUILayer;

import ControlLayer.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Product extends JFrame {

	private JPanel contentPane;
	private JTextField txtPname;
	private JTextField txtNetPrice;
	private JTextField txtTax;
	private JTextField txtMinStock;
	private JTextField txtStockAmount;
	private IFSearch search;
	private IFCreate create;
	private IFUpdate update;
	private ModelLayer.Product p;
	private boolean errorNP;
	private boolean errorT;
	private boolean errorMS;
	private boolean errorSA;
	

	/**
	 * Create the frame.
	 */
	public Product(boolean isNew, boolean isArchive, final int id) {
		
		search = new SearchControler();
		create = new CreateControler();
		update = new UpdateControler();
		
		setResizable(false);
		if(isNew)
		{
			setTitle("New Product");
		}
		else
		{
			setTitle("Product");
			p = search.getProductById(id);
		}
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(150, 50, 330, 360);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 94, 14);
		contentPane.add(lblId);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 36, 94, 14);
		contentPane.add(lblName);
		
		JLabel lblNettPrice = new JLabel("Nett price:");
		lblNettPrice.setBounds(10, 61, 94, 14);
		contentPane.add(lblNettPrice);
		
		JLabel lblTax = new JLabel("Tax (%):");
		lblTax.setBounds(10, 86, 94, 14);
		contentPane.add(lblTax);
		
		JLabel lblMinStock = new JLabel("Minimum stock:");
		lblMinStock.setBounds(10, 111, 94, 14);
		contentPane.add(lblMinStock);
		
		JLabel lblStockAmount = new JLabel("Stock amount:");
		lblStockAmount.setBounds(10, 136, 94, 14);
		contentPane.add(lblStockAmount);
		
		JLabel lblNewLabel = new JLabel("Description:");
		lblNewLabel.setBounds(10, 161, 94, 14);
		contentPane.add(lblNewLabel);
		
		final JEditorPane dtrpnDescription = new JEditorPane();
		dtrpnDescription.setBounds(114, 161, 190, 124);
		if(!isNew)
		{
			dtrpnDescription.setText(p.getDescription());
			if(isArchive)
			{
				dtrpnDescription.setEditable(false);
			}
		}
		contentPane.add(dtrpnDescription);
		
		JLabel lblPID = new JLabel("");
		lblPID.setBounds(114, 11, 86, 14);
		contentPane.add(lblPID);
		lblPID.setText(Integer.toString(id));
		
		txtPname = new JTextField();
		txtPname.setBounds(114, 33, 86, 20);
		contentPane.add(txtPname);
		txtPname.setColumns(10);
		if(!isNew)
		{
			txtPname.setText(p.getName());
			if(isArchive)
			{
				txtPname.setEditable(false);
			}
		}
		
		txtNetPrice = new JTextField();
		txtNetPrice.setBounds(114, 58, 86, 20);
		contentPane.add(txtNetPrice);
		txtNetPrice.setColumns(10);
		if(isNew!=true)
		{
			txtNetPrice.setText(Double.toString(p.getNettPrice()));
			if(isArchive)
			{
				txtNetPrice.setEditable(false);
			}
		}
		
		txtTax = new JTextField();
		txtTax.setBounds(114, 83, 86, 20);
		contentPane.add(txtTax);
		txtTax.setColumns(10);
		if(isNew!=true)
		{
			txtTax.setText(Integer.toString(p.getTax()));
			if(isArchive)
			{
				txtTax.setEditable(false);
			}
		}
		
		txtMinStock = new JTextField();
		txtMinStock.setBounds(114, 108, 86, 20);
		contentPane.add(txtMinStock);
		txtMinStock.setColumns(10);
		if(isNew!=true)
		{
			txtMinStock.setText(Integer.toString(p.getMinStock()));
			if(isArchive)
			{
				txtMinStock.setEditable(false);
			}
		}
		
		txtStockAmount = new JTextField();
		txtStockAmount.setBounds(114, 133, 86, 20);
		contentPane.add(txtStockAmount);
		txtStockAmount.setColumns(10);
		if(isNew!=true)
		{
			txtStockAmount.setText(Integer.toString(p.getStockAmount()));
			if(isArchive)
			{
				txtStockAmount.setEditable(false);
			}
		}
		
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancel.setBounds(215, 296, 89, 23);
		contentPane.add(btnCancel);
		
		if(!isArchive)
		{
			JButton btnSave = new JButton("Save");
			if(isNew){
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					saveProduct(id, txtPname.getText(), txtNetPrice.getText(), txtTax.getText(), txtMinStock.getText(), txtStockAmount.getText(), dtrpnDescription.getText());
				}
			});}
			else
			{
				btnSave.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						saveModification(id, txtPname.getText(), txtNetPrice.getText(), txtTax.getText(), txtMinStock.getText(), txtStockAmount.getText(), dtrpnDescription.getText());
					}
				});		
			}
			btnSave.setBounds(10, 296, 89, 23);
			contentPane.add(btnSave);
		}
		else
		{
			JButton btnActive = new JButton("Activate");
			btnActive.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					p.setStatus("active");
					saveActive(p);
					dispose();
					MainWindow.getInstance().refreshTableProduct();
					MainWindow.getInstance().refreshAP();
				}
			});
			btnActive.setBounds(10, 296, 89, 23);
			contentPane.add(btnActive);
		}
		
		
	}
	
	private void saveActive(ModelLayer.Product p)
	{
		update.updateProduct(p);
	}
	
	private void saveProduct(int id, String name, String netPrice, String tax, String minStock, String stockAmount, String description) 
	{
		if(name.isEmpty() || netPrice.isEmpty() || tax.isEmpty() || minStock.isEmpty() || stockAmount.isEmpty() || description.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
			errorNP = false;
			try
			{
				Double.parseDouble(netPrice);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,
	                    "The price has to be numeric!",
	                    "Warning",
	                    JOptionPane.WARNING_MESSAGE);
				errorNP = true;
			}
			if(!errorNP)
			{
				errorT = false;
				try
				{
					Integer.parseInt(tax);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
		                    "The tax has to be numeric!",
		                    "Warning",
		                    JOptionPane.WARNING_MESSAGE);
					errorT = true;
				}
				if(!errorT)
				{
					errorMS = false;
					try
					{
						Integer.parseInt(minStock);
					}
					catch(Exception e)
					{
						JOptionPane.showMessageDialog(null,
			                    "The minimum stock amount has to be numeric!",
			                    "Warning",
			                    JOptionPane.WARNING_MESSAGE);
						errorMS = true;
					}
					if(!errorMS)
					{
						errorSA = false;
						try
						{
							Integer.parseInt(stockAmount);
						}
						catch(Exception e)
						{
							JOptionPane.showMessageDialog(null,
				                    "The stock amount has to be numeric!",
				                    "Warning",
				                    JOptionPane.WARNING_MESSAGE);
							errorSA = true;
						}
						if(!errorSA)
						{
							double netP = Double.parseDouble(netPrice);
							int taxX = Integer.parseInt(tax);
							int min = Integer.parseInt(minStock);
							int stock = Integer.parseInt(stockAmount);
							ModelLayer.Product p = new ModelLayer.Product(id, name, netP, taxX, min, stock, description, "active");
							try {
								create.createProduct(p);
							} catch (Exception e) {
								JOptionPane.showMessageDialog(null,
		                                "Something went wrong, please try again!",
		                                "Warning",
		                                JOptionPane.WARNING_MESSAGE);
								e.printStackTrace();
								
							}
							MainWindow.getInstance().refreshTableProduct();
							dispose();
						}
					}
				}
			}
		}
	}
	
	private void saveModification(int id, String name, String netPrice, String tax, String minStock, String stockAmount, String description)
	{
		if(name.isEmpty() || netPrice.isEmpty() || tax.isEmpty() || minStock.isEmpty() || stockAmount.isEmpty() || description.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
			errorNP = false;
			try
			{
				Double.parseDouble(netPrice);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,
	                    "The price has to be numeric!",
	                    "Warning",
	                    JOptionPane.WARNING_MESSAGE);
				errorNP = true;
			}
			if(!errorNP)
			{
				errorT = false;
				try
				{
					Integer.parseInt(tax);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
		                    "The tax has to be numeric!",
		                    "Warning",
		                    JOptionPane.WARNING_MESSAGE);
					errorT = true;
				}
				if(!errorT)
				{
					errorMS = false;
					try
					{
						Integer.parseInt(minStock);
					}
					catch(Exception e)
					{
						JOptionPane.showMessageDialog(null,
			                    "The minimum stock amount has to be numeric!",
			                    "Warning",
			                    JOptionPane.WARNING_MESSAGE);
						errorMS = true;
					}
					if(!errorMS)
					{
						errorSA = false;
						try
						{
							Integer.parseInt(stockAmount);
						}
						catch(Exception e)
						{
							JOptionPane.showMessageDialog(null,
				                    "The stock amount has to be numeric!",
				                    "Warning",
				                    JOptionPane.WARNING_MESSAGE);
							errorSA = true;
						}
						if(!errorSA)
						{
							double netP = Double.parseDouble(netPrice);
							int taxX = Integer.parseInt(tax);
							int min = Integer.parseInt(minStock);
							int stock = Integer.parseInt(stockAmount);
							ModelLayer.Product p = new ModelLayer.Product(id, name, netP, taxX, min, stock, description, "Active");
							update.updateProduct(p);
							dispose();
						}
					}
				}
			}
		}
	}
}
